package com.example.wi_fipartyandroid;

import androidx.activity.result.ActivityResultCallback;
import androidx.activity.result.ActivityResultLauncher;
import androidx.activity.result.contract.ActivityResultContracts;
import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.media.AudioPlaybackCaptureConfiguration;
import android.media.AudioRecord;
import android.media.MediaRecorder;
import android.media.projection.MediaProjectionManager;
import android.net.Uri;
import android.net.wifi.WifiManager;
import android.os.Bundle;
import android.util.Log;
import android.widget.CompoundButton;
import android.widget.Switch;
import android.widget.TextView;

import com.example.wi_fipartyandroid.databinding.ActivityMainBinding;

public class MainActivity extends AppCompatActivity {

    // Used to load the 'wi_fipartyandroid' library on application startup.
    static {
        System.loadLibrary("wi_fipartyandroid");
    }

    private ActivityMainBinding binding;
    private boolean created=false;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        if(created){
            Log.println(Log.INFO,"MainActivity.java","onCreate called again!");
            return;
        }
        Log.println(Log.INFO,"MainActivity.java","onCreate() Run");
        super.onCreate(savedInstanceState);

        binding = ActivityMainBinding.inflate(getLayoutInflater());
        setContentView(binding.getRoot());

        // Example of a call to a native method
        TextView tv = binding.sampleText;

        WifiManager wm= (WifiManager) getSystemService(WIFI_SERVICE);
        WifiManager.WifiLock lock=wm.createWifiLock(WifiManager.WIFI_MODE_FULL_HIGH_PERF,"Wi-Fi Party Android");
        WifiManager.MulticastLock multicastLock = wm.createMulticastLock("Wi-Fi Party Android");

        initializeAudioEngine();
        startPlaying();

        Switch singSwitch=binding.singSwitch;
        singSwitch.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
                if(b) startRecording();
                else stopRecording();
            }
        });

//        ActivityResultContracts.
//        Switch audioCaptureSwitch=binding.captureAppAudio;
//        audioCaptureSwitch.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
//            @Override
//            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
//                MediaProjectionManager mmgr=(MediaProjectionManager) getSystemService(MEDIA_PROJECTION_SERVICE);
//                Intent getp=mmgr.createScreenCaptureIntent();
//                startActivityForResult(getp,1); //1: request media projection
//                AudioPlaybackCaptureConfiguration conf= AudioPlaybackCaptureConfiguration.Builder.build();
//                AudioRecord.Builder arb=new AudioRecord.Builder()
//                        .setAudioPlaybackCaptureConfig(conf)
//
//
//
//            }
//        });
        Log.println(Log.INFO,"MainActivity.java","Finished onCreate()");
        created=true;
    }

    /**
     * A native method that is implemented by the 'wi_fipartyandroid' native library,
     * which is packaged with this application.
     */
    public native String stringFromJNI();
    private native void startPlaying();
    private native void stopPlaying();
    private native void startRecording();
    private native void stopRecording();
    private native void test_slab();
    private native void initializeAudioEngine();
}